package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;


    @GetMapping
    public Result list(StudentQueryParam student) {
        log.info("查询所有学生信息：{}", student);
        //开启分页查询
        Page<Student> page = new Page(student.getPage(),student.getPageSize());
        //设置分页查询的排序
        page.addOrder(OrderItem.desc("update_time"));
        //根据请求参数编写sql语句的条件
        LambdaQueryWrapper<Student> sqw = new LambdaQueryWrapper<>();
        sqw.like(student.getName()!= null && student.getName().length()>0,Student::getName,student.getName())
            .eq(student.getDegree()!= null,Student::getDegree,student.getDegree())
            .eq(student.getClazzId()!= null,Student::getClazzId,student.getClazzId());
        page = studentService.page(page,sqw);
        List<Student> result = page.getRecords();
        PageResult pageResult = new PageResult(page.getTotal(), result);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学生信息：{}", student);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentService.saveStudent(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学生信息：{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping("/")
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息：{}", student);


        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据id删除学生信息：{}", ids);
        studentService.removeBatchByIds(ids);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据id{}扣分, 扣分：{}", id, score);
        studentService.violationCount(id,score);
        return Result.success();
    }
}
